package com.meteor.downloader.videodownloader.activity

import android.content.Context
import android.os.Handler
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdView
import com.hjq.language.MultiLanguages
import com.meteor.downloader.videodownloader.admdm.MdmDealAdUtils
import com.meteor.downloader.videodownloader.admdm.ShowPageAdListener
import com.meteor.downloader.videodownloader.databinding.LayoutAdSizeCenterUiAaBinding
import com.meteor.downloader.videodownloader.databinding.LayoutAdSizeSmallUiAaBinding
import com.meteor.downloader.videodownloader.utils.MdmTotalUtils
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 热启动
 * 下载有时候会很久没反应
 * 控制参数
 * fb+x+pinterest
 */
abstract class MdmRootActivity : AppCompatActivity() {
    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(MultiLanguages.attach(newBase))
    }

    protected var mHandlerMdm: Handler? = null
    protected var mPagePause = false
    override fun onResume() {
        mPagePause = false
        super.onResume()
        if (mShowNativeAgain) {
            mShowNativeAgain = false
            showNativeCodeAd()
        } else {
            if (mStarNativeValue) {
                loadNativeCodeAd()
            }
        }
    }

    override fun onPause() {
        mPagePause = true
        super.onPause()
    }


    // show ad again
    protected var mRestartAdShow = false

    private var mNativeMdmValue: NativeAd? = null

    //是否加载native
    protected var mStarNativeValue = false

    //归因
    protected var mStatusLook = false
    private var mNativeTimeLet = 0L
    protected var mNativeBookLayout: FrameLayout? = null
    private var mShowFirst = false
    protected var mNativeViewType = "sizeCenter"
    private var mBookShowing = false
    private var mShowNativeAgain = false

    private fun loadNativeCodeAd() {
        lifecycleScope.launch {
            delay(360)
            if (mStatusLook && !MdmTotalUtils.getMdmAppStatus()) {
                return@launch
            }
            if (System.currentTimeMillis() - mNativeTimeLet > 14000) {
                mBookShowing = false
                if (!mShowFirst) {
                    when (mNativeViewType) {
                        "sizeSmall" -> {
                            mNativeBookLayout?.addView(
                                LayoutAdSizeSmallUiAaBinding.inflate(
                                    layoutInflater
                                ).root
                            )
                        }

                        "sizeCenter" -> {
                            mNativeBookLayout?.addView(
                                LayoutAdSizeCenterUiAaBinding.inflate(
                                    layoutInflater
                                ).root
                            )
                        }
                    }
                    mShowFirst = true
                }
                MdmDealAdUtils.loadBookNativeMdm(true, mStatusLook)
            }
        }
    }

    private fun showNativeCodeAd() {
        if (isFinishing || isDestroyed) return
        if (mNativeBookLayout == null) return
        if (mBookShowing) return
        if (mPagePause) {
            mShowNativeAgain = true
            return
        }
        mBookShowing = true
        mNativeTimeLet = System.currentTimeMillis()
        try {
            MdmDealAdUtils.showTionNativeValue(
                this,
                mNativeViewType,
                mStatusLook,
                object : ShowPageAdListener() {
                    override fun nativeAddShowBack(adView: NativeAdView?, nativeAd: NativeAd?) {
                        if (adView != null) {
                            mNativeBookLayout?.removeAllViews()
                            mNativeBookLayout?.addView(adView)
                            mNativeMdmValue = nativeAd
                        }
                    }
                })
        } catch (e: Exception) {
        }
    }

    //
    open fun loadBookNativeMdm(result: Boolean) {
        if (mStatusLook && !MdmTotalUtils.getMdmAppStatus()) {
            return
        }
        if (mStarNativeValue) {
            showNativeCodeAd()
        }
    }

    open fun loadBookIntersMdm(result: Boolean) {

    }

    open fun loadBookOpenMdm(result: Boolean) {

    }

    open fun showRootBookAdBack(type: String, statusLook: Boolean, mdmBack: () -> Unit) {
        MdmDealAdUtils.showTionIntersValue(
            this,
            type,
            statusLook,
            object : ShowPageAdListener() {
                override fun dismissAFailed() {
                    mdmBack()
                }
            })
    }
    override fun onDestroy() {
        super.onDestroy()
        mHandlerMdm?.removeCallbacksAndMessages(null)
        mHandlerMdm = null
    }
}